package com.smsc.headend.task.engine.handler.impl.timesync;

import com.smsc.headend.module.protocol.DeviceData;
import com.smsc.headend.module.report.entity.LogScheduleTaskDevice;
import com.smsc.headend.module.task.dto.AtomicTask;
import com.smsc.headend.module.task.dto.Task;
import com.smsc.headend.task.engine.dao.TaskDAO;
import com.smsc.headend.task.engine.exception.UDISTaskHandlerException;
import com.smsc.headend.task.engine.handler.TaskHandler;
import com.smsc.headend.task.engine.service.task.LogScheduleTaskDeviceService;
import org.springframework.beans.factory.annotation.Autowired;

import java.lang.reflect.InvocationTargetException;

public class SetClockHandler implements TaskHandler {
    @Autowired
    TaskDAO taskDAO;

    @Autowired
    LogScheduleTaskDeviceService logScheduleTaskDeviceService;

    @Override
    public int handle(Task task, AtomicTask atomicTask, DeviceData cosemData, String actionResult) throws UDISTaskHandlerException, InvocationTargetException, NoSuchMethodException, InstantiationException, IllegalAccessException {
        log.debug("set clock: taskNo={}", task.getTaskNo());
//        logScheduleTaskDeviceService.sendMessage(task, LogScheduleTaskDevice.StatusEnum.Success.getCode(), LogScheduleTaskDevice.StatusDescEnum.CollectedSuccess.getCode());
        return 1;
    }

    @Override
    public int handleFailure(Task task, AtomicTask atomicTask, String message) {
        taskDAO.addTaskData(task.getTaskNo(), atomicTask.getAtomicTaskNo(), "message", message, 3600l, true);
        taskDAO.addTaskData(task.getTaskNo(), atomicTask.getAtomicTaskNo(), "result", 2, 3600l, true);
        logScheduleTaskDeviceService.sendMessage(task, LogScheduleTaskDevice.StatusEnum.Failed.getCode(), LogScheduleTaskDevice.StatusDescEnum.DecodeDataFailed.getCode());
        return -1;
    }
}
